package com.example.game.module.mapper;

import com.example.game.module.entity.Category;
import org.apache.ibatis.annotations.*;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

@Mapper
public interface CategoryMapper {

    // 校验 parent_id 是否存在（手动维护数据完整性）
    @Select("SELECT COUNT(*) FROM category WHERE id = #{parentId}")
    boolean existsParentId(@Param("parentId") Long parentId);

    //模糊查询返回categoryId
    @Select("SELECT id FROM category WHERE name LIKE CONCAT('%', #{keyword}, '%') AND is_deleted = 0 ORDER BY id DESC LIMIT 99 ")
    List<BigInteger> findCategoryIdByKeyword(@Param("keyword") String keyword);

    //根据ID批量查询
    @Select({"<script> " +
            "SELECT * FROM category WHERE id IN" +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script> "})
    List<Category> selectByIds(@Param("ids") Collection<Long> ids) ;
    //查询全部信息
    @Select("select * from category WHERE is_deleted = 0")
    List<Category> findAll();

    // 根据ID查询操作
    @Select("SELECT * FROM category WHERE id = #{categoryId} AND is_deleted=0")
    Category getById(Long categoryId);

    // 根据ID提取操作
    @Select("SELECT * FROM category WHERE id = #{categoryId} ")
    List<Category> extractById(Long categoryId);

    // 插入操作
    Integer insert(Category category);

    // 更新操作
    Integer update(Category category);

    // 统计子分类数量（删除保护）
    @Select("SELECT COUNT(*) FROM category WHERE parent_id = #{id}")
    Integer countChildren(@Param("id") Long id);

    // 删除操作
    @Update("UPDATE category SET update_time = #{updateTime} , is_deleted = 1 WHERE id = #{categoryId}")
    Integer delete(@Param("updateTime") Integer updateTime, @Param("categoryId")  Long categoryId);

}

